package com.skyline.common.util;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import com.skyline.base.type.MailType;
import com.skyline.base.type.NotificationType;
import com.skyline.base.type.ShareType;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtils {
	private static final Log LOGGER = LogFactory.getLog(FreeMarkerUtils.class);
	private static Map<MailType, Template> mailContentTemplates = new HashMap<MailType, Template>();
	private static Map<MailType, Template> mailTitleTemplates = new HashMap<MailType, Template>();
	private static Map<NotificationType, Template> notificationTemplates = new HashMap<NotificationType, Template>();
	private static final Configuration CONFIG = new Configuration();

	static {
		CONFIG.setDefaultEncoding("UTF-8");
		ClassTemplateLoader[] loaders = new ClassTemplateLoader[] {
				new ClassTemplateLoader(FreeMarkerUtils.class, "/config/template/mail"),
				new ClassTemplateLoader(FreeMarkerUtils.class, "/config/template/notification") ,
				new ClassTemplateLoader(FreeMarkerUtils.class, "/config/template/share"),
				new ClassTemplateLoader(FreeMarkerUtils.class, "/config/template/resource"),
				new ClassTemplateLoader(FreeMarkerUtils.class, "/config/template/shortMessage")};
		MultiTemplateLoader templateLoader = new MultiTemplateLoader(loaders);
		CONFIG.setTemplateLoader(templateLoader);
	}

	private static Template getNotificationTemplate(NotificationType type) {
		String filename = null;
		switch (type) {
		case ALBUM_COMMENT:
			filename = "albumcomment.ftl";
			break;
		case ARTICLE_COMMENT:
			filename = "artilecomment.ftl";
			break;
		case PHOTO_COMMENT:
			filename = "photocomment.ftl";
			break;
		case SHARE_COMMENT:
			filename = "sharecomment.ftl";
			break;
		case FOLLOW:
			filename = "follow.ftl";
			break;
		case MANAGE_NOTIFICATION:
			filename = "managenotification.ftl";
			break;
		// TODO:增加其他的类型映射
		default:
			filename = null;
		}
		return getTemplate(filename);
	}

	private static Template getMailContentTemplate(MailType type) {
		String filename = null;
		switch (type) {
		case RIGIST:
			filename = "regist-content.ftl";
			break;
		case FORGET_PASSWORD:
			filename = "forgetpwd-content.ftl";
			break;
		case INVITE:
			filename = "invite-content.ftl";
			break;
		// TODO:增加其他的类型映射
		default:
			filename = null;
			break;
		}
		return getTemplate(filename);
	}

	private static Template getMailTitleTemplate(MailType type) {
		String filename = null;
		switch (type) {
		case RIGIST:
			filename = "regist-title.ftl";
			break;
		case FORGET_PASSWORD:
			filename = "forgetpwd-title.ftl";
			break;
		case INVITE:
			filename = "invite-title.ftl";
			break;
		// TODO:增加其他的类型映射
		default:
			filename = null;
		}
		return getTemplate(filename);
	}

	private static Template getTemplate(String filename) {
		if (!StringUtils.hasLength(filename)) {
			return null;
		}
		try {
			return CONFIG.getTemplate(filename);
		} catch (IOException e) {
			LOGGER.warn("获取模板文件" + filename + "出错！", e);
			return null;
		}
	}

	private static Template getShareTemplate(ShareType type) {
		String fileName = null;
		switch (type) {
		case PHOTO:
			fileName = "photo-share.ftl";
			break;
		default:
			fileName = null;
		}
		return getTemplate(fileName);
	}

	private static Writer getWriter() {
		return new StringWriter();
	}
	
	private static String processTemplate(Template template, Map<String, Object> valueMap) {
		if (template == null) {
			return null;
		}
		Writer out = getWriter();
		try {
			template.process(valueMap, out);
		} catch (TemplateException e) {
			LOGGER.warn("转换模板遇到错误", e);
		} catch (IOException e) {
			LOGGER.warn("写入结果到字符串发生错误", e);
		}
		return out.toString();

	}

	private static String getMailTitle(MailType type, Map<String, Object> valueMap) {
		Template template = mailTitleTemplates.get(type);
		if (template == null) {
			template = getMailTitleTemplate(type);
			if (template != null) {
				mailTitleTemplates.put(type, template);
			}
		}
		return processTemplate(template, valueMap);
	}

	private static String getMailContent(MailType type, Map<String, Object> valueMap) {
		Template template = mailContentTemplates.get(type);
		if (template == null) {
			template = getMailContentTemplate(type);
			if (template != null) {
				mailContentTemplates.put(type, template);
			}
		}
		return processTemplate(template, valueMap);
	}

	public static String getNotification(NotificationType type, Map<String, Object> valueMap) {
		Template template = notificationTemplates.get(type);
		if (template == null) {
			template = getNotificationTemplate(type);
			if (template != null) {
				notificationTemplates.put(type, template);
			}
		}
		return processTemplate(template, valueMap);
	}

	public static String getShare(ShareType type, Map<String, Object> valueMap) {
		Template template = getShareTemplate(type);
		return processTemplate(template, valueMap);
	}

	public static String getRegistMailTitle(String nickname) {
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("username", nickname);
		return getMailTitle(MailType.RIGIST, valueMap);
	}

	public static String getRegistMailContent(String nickname, String verifyUrl) {
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("username", nickname);
		valueMap.put("verifyurl", verifyUrl);
		valueMap.put("date", new Date());
		return getMailContent(MailType.RIGIST, valueMap);
	}
	
	public static String getForgetPasswordMailTitle(String email){
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("email", email);
		return getMailTitle(MailType.FORGET_PASSWORD, valueMap);
	}
	
	public static String getForgetPasswordMailContent(String email,String token){
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("email", email);
		valueMap.put("token", 	token);
		valueMap.put("date", new Date());
		return getMailContent(MailType.FORGET_PASSWORD, valueMap);
	}
	
	public static String getVideoDigest(Map<String, Object> valueMap){
		Template template = getTemplate("video.ftl");
		return processTemplate(template,valueMap);
	}
	
	public static String getShortMessage(String text, Long sourceOwnerId,
			String sourceOwnerNickname, String comment, List<Long> commentterIds, List<String> commentterNicknames, List<String> comments){
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("text", text);
		valueMap.put("sourceOwnerId", 	sourceOwnerId);
		valueMap.put("sourceOwnerNickname", sourceOwnerNickname);
		valueMap.put("comment", comment);
		valueMap.put("commentterIds", commentterIds);
		valueMap.put("commentterNicknames", commentterNicknames);
		valueMap.put("comments", comments);
		Template template=getTemplate("shortMessage.ftl");
		//System.out.println(valueMap);
		//System.out.println(template);
		return processTemplate(template, valueMap);
	}

	// public static String get

	public static void main(String... args) throws IOException, TemplateException {
		String con=FreeMarkerUtils.getShortMessage("text", 1l," sourceOwnerNickname"," comment", null, null, null);
		System.out.println(con);
	}
}
